<template>
  <div class="tktk_container">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="新闻标题" prop="headline">
        <el-input v-model="ruleForm.headline" maxlength="30" class="w260"></el-input>
      </el-form-item>
      <el-form-item label="新闻简介" prop="textDesc">
        <el-input type="textarea" v-model="ruleForm.textDesc" show-word-limit maxlength="120" class="w260"></el-input>
      </el-form-item>
      <el-form-item label="是否显示" prop="showFlag">
        <div class="radio-group">
          <label><input value="0" v-model="ruleForm.showFlag" type="radio" name="status" />显示</label>
          <label><input value="1" v-model="ruleForm.showFlag" type="radio" name="status" />隐藏</label>
        </div>
      </el-form-item>
      <el-form-item label="新闻内容" prop="textPart">
        <editor v-model="ruleForm.textPart" :min-height="300" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
        <el-button @click="reset('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { newsAdd, newsUpdate, newsDetail } from '@/api/tktk/news.js'
export default {
  data() {
    return {
      ruleForm: {
        headline: '',
        showFlag: 0,
        textDesc: '',
        textPart: ''
      },
      rules: {
        headline: [
          { required: true, message: '新闻标题不能为空', trigger: 'blur' }
        ],
        textDesc: [
          { required: true, message: '新闻简介不能为空', trigger: 'blur' }
        ],
        showFlag: [
          { required: true, message: '请选择是否显示', trigger: 'change' }
        ],
        textPart: [
          { required: true, message: '新闻内容不能为空', trigger: 'blur' }
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const { id } = this.$route.query
          if (id) {
            newsUpdate(this.ruleForm).then(res => {
              if (res.code === 200) {
                this.$message.success('修改成功')
                this.$router.push('/news/list')
              } else {
                this.$message.error(res.msg)
              }
            })
          } else {
            newsAdd(this.ruleForm).then(res => {
              if (res.code === 200) {
                this.$message.success('新增成功')
                this.$router.push('/news/list')
              } else {
                this.$message.error(res.msg)
              }
            })
          }
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  },

  mounted() {
    const { id } = this.$route.query
    if (id) {
      newsDetail({ id }).then(res => {
        if (res.code === 200) {
          this.ruleForm = res.data
        } else {
          this.$message.error(res.msg)
          this.$router.push('/news/list')
        }
      })
    }
  }
}
</script>
